﻿using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.Text;

namespace Common.Swagger.Filter
{
    public class LongDocumentFilter : IDocumentFilter
    {
        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            SetLongToString(swaggerDoc.Components.Schemas);
        }
        private void SetLongToString(IDictionary<string, OpenApiSchema> components)
        {
            if (components == null || components.Count <= 0)
            {
                return;
            }
            foreach (var kv in components)
            {
                if (kv.Value.Properties == null || kv.Value.Properties.Count <= 0)
                {
                    if (kv.Value.Format == "int64")
                    {
                        kv.Value.Type = "string";
                    }
                }
                else
                {
                    SetLongToString(kv.Value.Properties);
                }
            }
        }
    }

}
